﻿using System.ComponentModel.Composition;
using System.Windows.Input;
using SL4_MVVM_MEF.MEFModule.Model;
using SL4_MVVM_MEF.MEFModule.Services;
using SL4_MVVM_MEF.MEFModule.Helper;
using Microsoft.Practices.Composite.Events;

namespace SL4_MVVM_MEF.MEFModule.ViewModels
{
    /// <summary>
    /// ViewModel class for the MainPageView
    /// </summary>
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Export(ExportNames.SampleViewVM)]
    public class SampleViewModel : ViewModel, ISampleViewModel
    {
        /// <summary>
        /// Instance of the Event Aggregator
        /// </summary>
        private readonly IEventAggregator _eventAggregator;
        private readonly IDataItemsService _dataItemsService;

        /// <summary>
        /// Imported constructor
        /// </summary>
        [ImportingConstructor]
        public SampleViewModel(IEventAggregator eventAggregator, IDataItemsService dataItemsService)
        {
            _eventAggregator = eventAggregator;
            _dataItemsService = dataItemsService;

            //Subscribe to the "DataItemsReceivedEvent"
            _eventAggregator.GetEvent<DataItemsReceivedEvent>().Subscribe(
                dataItemsReceived =>
                {
                    if (dataItemsReceived != null)
                    {
                        this.dataItems = dataItemsReceived;
                    }
                }, true);

            //Load Data
            _dataItemsService.GetDataItems();
        }

        /// <summary>
        /// A sample property
        /// </summary>
        [Import(ExportNames.aViewModelPropertyTextProvider)]
        public string aViewModelProperty 
        { 
            get { return _aViewModelProperty; }
            set 
            { 
                _aViewModelProperty = value; 
                RaisePropertyChanged(()=>this.aViewModelProperty); 
            } 
        }
        private string _aViewModelProperty;

        /// <summary>
        /// A sample collection
        /// </summary>
        public DataItems dataItems 
        { 
            get { return _dataItems; }
            set 
            { 
                _dataItems = value; 
                RaisePropertyChanged(()=>this.dataItems);
            } 
        }
        private DataItems _dataItems;

        /// <summary>
        /// IsLoading property
        /// </summary>
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                _isLoading = value;
                RaisePropertyChanged(()=>this.IsLoading);
            }
        }
        private bool _isLoading;

        /// <summary>
        /// A sample Command
        /// </summary>
        [Import(typeof(ICommand))]
        public ICommand addDataItemCommand { get; set; }
    }
}
